<?=view('header.html')?>

<body class="layui-view-body">
    <div class="layui-content">
        <div class="layui-row">
            <div class="layui-card">

                <div class="layui-card-header layui-form layui-form-item">
                    <div class="layui-inline">
                        <form class="layui-form" lay-filter="search-form" id="search" name="searh">

                            <div class="layui-form-mid">用户名:</div>
                            <div class="layui-input-inline" style="width: 140px;">
                                <input type="text" name="username" value="" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid">姓名:</div>
                            <div class="layui-input-inline" style="width: 140px;">
                                <input type="text" name="name" value="" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid">邮箱:</div>
                            <div class="layui-input-inline" style="width: 140px;">
                                <input type="text" name="email" value="" autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid">手机号:</div>
                            <div class="layui-input-inline" style="width: 100px;">
                                <input type="text" name="phone" value="" autocomplete="off" class="layui-input">
                            </div>
                            <button type="button" class="layui-btn layui-btn-blue" lay-submit lay-filter="search">查询</button>
                            <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="search_reset">重置</button>
                        </form>
                    </div>
                </div>

                <div class="layui-card-body">

                    <table id="userlist" lay-filter="userlist"></table>

                    <!--批量操作-->
                    <script type="text/html" id="batchbar">
                        <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delAll">删除</button>
                        <a class="layui-btn layui-btn-sm" lay-event="add">添加管理员</a>
                    </script>

                    <!--行操作-->
                    <script type="text/html" id="troption">
                    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                    {{# if(d.uid!=1){ }}
                    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    {{# } }}
                </script>

                </div>
            </div>
        </div>
    </div>
    <?=view('footer.html')?>

    <script type="text/javascript">

        layui.use(['layer', 'form', 'jquery', 'table'], function () {
            var layer = layui.layer,
                $ = layui.jquery,
                form = layui.form,
                table = layui.table;
            

            //列表
            var tableIns = table.render({
                elem: '#userlist'
                , url: adminpath + "admin/rbac/index"
                , defaultToolbar: []
                , method: 'post'
                , page: {
                    layout: ['prev', 'page', 'next', 'count'],
                    groups: 5,
                    first: true,
                    last: true
                },
                response: {
                    statusCode: 0,
                    countName: 'count'
                },
                parseData: function (res) {
                    return {
                        'code': res.code,
                        "data": res.data.data,
                        'count': res.data.total
                    };
                }
                , toolbar: '#batchbar'
                , cols: [[
                    { type: 'checkbox' }
                    , { field: 'uid', title: 'ID', width: 80, sort: true }
                    , { field: 'username', title: '用户名' }
                    , { field: 'name', title: '姓名' }
                    , { field: 'role_name', title: '角色组' }
                    , { field: 'email', title: '邮箱' }
                    , { field: 'phone', title: '手机号' }
                    , { field: 'logindate', title: '最后登录时间' }
                    , { field: 'loginip', title: '最后登录ip' }
                    , { field: 'logindate', title: '添加时间' }
                    , { fixed: 'right', title: '操作', toolbar: '#troption' }
                ]]
                , done: function (curr, count) {
                    // $("#ams_total").val(count);
                }
            });

            //查询提交
            form.on('submit(search)', function (data) {
                tableInit.reload({
                    where: data.field
                    , page: {
                        curr: 1
                    }
                });
            });

            //重置search_reset
            form.on('submit(search_reset)', function (data) {
                $('#search')[0].reset();
                form.render(null, 'search-form');
                var field = data.field;
                $.each(field, function (k, v) {
                    field[k] = '';
                });
                tableInit.reload({
                    where: data.field
                    , page: {
                        curr: 1
                    }
                });
            });

            //头工具栏事件
            table.on('toolbar(userlist)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                var ids = [];
                $.each(data, function (i, v) {
                    ids.push(v.uid);
                })

                switch (obj.event) {
                    case 'delAll':

                        layer.confirm('确定要删除吗？', { icon: 3, title: '消息提示' }, function (index) {
                            $.post(adminpath + 'admin/rbac/del', { ids: ids }, function (res) {
                                if (res.code == 0) {
                                    layer.msg(res.msg, { time: 1500 });
                                } else {
                                    layer.msg(res.msg);
                                }
                                tableIns.reload();
                            }, 'json')
                            layer.close(index);
                        });
                        break;

                    case 'add':
                        layer.open({
                            title: '添加管理员',
                            type: 2,
                            area: ['600px', '90%'],
                            anim: 2,
                            shadeClose: false,
                            content: adminpath + 'admin/rbac/add'
                        });
                        break;

                }
                ;
            });

            //监听行工具事件
            table.on('tool(userlist)', function (obj) {
                var data = obj.data;

                switch (obj.event) {

                    case 'del':
                        layer.confirm('确定要删除吗？', { icon: 3, title: '删除提示' }, function (index) {
                            $.post(adminpath + 'admin/rbac/del', { ids: data.uid }, function (res) {
                                if (res.code == 0) {
                                    layer.msg(res.msg, { time: 1500, });
                                } else {
                                    layer.msg(res.msg);
                                }
                                tableInit.reload();
                            }, 'json')
                            layer.close(index);
                        });
                        break;
                    case 'edit':
                        var url = adminpath + 'admin/rbac/edit?uid=' + data.uid;
                        layer.open({
                            title: '编辑管理员',
                            type: 2,
                            area: ['600px', '90%'],
                            anim: 2,
                            shadeClose: false,
                            content: url
                        });
                        break;

                }


            });


        });

    </script>
</body>

</html>
